console.log("运行必应积分读取");
const date = new Date();
const today = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
const hour = date.getHours() + ":" + date.getMinutes();

window.onload = function () {
    setTimeout(function () {
        getTodaySearchPoints();
        ff()
    }, 1000)
}

function getTodaySearchPoints() {
    //判断是否登录
    const subtitleDom = document.querySelectorAll(".b_subtitle");
    let textValue = '未登录';
    if (subtitleDom.length > 0) {
        for (let i = 0; i < subtitleDom.length; i++) {
            let text = subtitleDom[i].innerText;
            if (text.indexOf("你已获得") !== -1) {
                const regex = /\d+/g; // 匹配一个或多个数字，全局搜索
                const matches = text.match(regex);
                if (matches) {
                    textValue =`<span style="color: red;">${matches.map(Number)}</span> 积分`;
                } else {
                    textValue = '无法解析出积分';
                }
                break;
            }
        }
    }else{
        chrome.storage.local.set({progress: '1', searchDate: today, limitedDate: today, manualSearch: '0', isLock: '0'})
    }
    chrome.storage.local.set({todaySearchPoints: textValue}, () => {
        console.log("数据已存储");
    });
}

function ff() {
    const subtitleDom = document.querySelectorAll(".promo_cont a");
    if (subtitleDom.length > 0) {
        chrome.storage.local.get(['progress', 'manualSearch', 'isAutoSignIn', 'executionTime', 'searchDate'], (results) => {
            if (results.isAutoSignIn === "1" || results.manualSearch === "1") {
                let index=0;
                if (results.searchDate !== today && convertToMinutes(results.executionTime) < convertToMinutes(hour) && results.progress === "3") {
                    if (convertToMinutes("12:30") < convertToMinutes(hour)){//大于12点30的，就不进行二次打开了
                        chrome.storage.local.set({progress: '1', searchDate: today, limitedDate: today, manualSearch: '0', isLock: '0'})
                    }else{
                        chrome.storage.local.set({progress: '4', searchDate: today})
                    }
                    let setInt=setInterval(function(){
                        if(subtitleDom[index]!==undefined){
                            subtitleDom[index].click();
                            index++;
                        }else{
                            clearInterval(setInt);
                        }
                    },500);
                }

                if (results.limitedDate !== today && convertToMinutes("12:30") < convertToMinutes(hour) && results.progress === "4") {
                    chrome.storage.local.set({progress: '1', limitedDate: today, manualSearch: '0', isLock: '0'})
                    let setInt=setInterval(function(){
                        if(subtitleDom[index]!==undefined){
                            subtitleDom[index].click();
                            index++;
                        }else{
                            clearInterval(setInt);
                        }
                    },500);
                }
            }
        });
    }
}

function convertToMinutes(timeString) {
    const [hours, minutes] = (timeString ?? ':').split(':').map(Number);
    return hours * 60 + minutes;
}